﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Configuration;
using System.Xml;
using System.Xml.XPath;

namespace repliback4.Misc {
    public class Field {
        XPathNavigator navi;
        public Field ( XPathNavigator navi ) {
            this.navi = navi;
        }
        public override string ToString ( ) {
            return this.navi.Value;
        }

        public static implicit operator string ( Field f ) {
            return f.ToString( );
        }

        public int AsInt {
            get {
                return int.Parse( this.ToString( ) );
            }
        }
    }
    public class ConfigFile {

        protected static Dictionary<String, ConfigFile> configs = new Dictionary<string, ConfigFile>( );
        protected XmlDocument xDoc = new XmlDocument( );
        protected XPathNavigator xpn;

        protected String section;

        public ConfigFile ( String fileName, String section ) {
            this.xDoc.Load( fileName );
            this.xpn = xDoc.CreateNavigator( );
            this.section = section;
        }

        public Field this[String key] {
            get {
                String xname;
                if ( this.section != "" ) {
                    xname = String.Format( "/configuration/{0}/{1}/text()", this.section, key );
                } else {
                    xname = String.Format( "/configuration/{1}/text()", this.section, key );
                }
                return this.GetField( xname );
            }
        }
        public Field GetField ( String name ) {

            XPathNodeIterator i = xpn.Select( name );
            i.MoveNext( );
            if ( i != null ) {
                return new Field( i.Current );
            } else {
                return null;
            }
        }

    }
}
